package com.jsnkj.basic.activity

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.jsnkj.basic.R
import com.jsnkj.basic.adapter.MyApprenticeAdapter
import com.jsnkj.basic.databinding.ActivityMyApprenticeBinding
import com.jsnkj.basic.mvvm.BaseKtActivity


//我的徒弟
class MyApprenticeActivity :
    BaseKtActivity<ActivityMyApprenticeBinding, MyApprenticeViewModel>() {

    override val layoutId: Int
        get() = R.layout.activity_my_apprentice


    private var apprenticeAdapter: MyApprenticeAdapter? = null

    override fun initView(savedInstanceState: Bundle?) {
        mVDB.topTitle.txtTopTitle.text = "我的徒弟"
        mVDB.topTitle.imageReturn.setOnClickListener {
            onBackPressed()
        }

        showDialog()
        mVM.postApprentice()

    }


    override fun bindEvent() {

        super.bindEvent()

        mVM.errorResult.observe(this) {

            dismissDialog()
            com.jsnkj.basic.util.ToastUtils.show(it.message)
        }

        mVM.postHttpResult.observe(this) { entity ->
            dismissDialog()


            mVDB.txtApprentice.text = "${entity.total}"
            mVDB.txtMoney.text = "${entity.commissions}"

            com.jsnkj.basic.util.LogUtils.e("请求数据 ：" + entity.disciples?.size)
            if (null == apprenticeAdapter) {
                mVDB.rvApprentice.setLayoutManager(
                    LinearLayoutManager(
                        this@MyApprenticeActivity,
                        LinearLayoutManager.VERTICAL,
                        false
                    )
                )
                if (null == entity.disciples) {
                    entity.disciples = ArrayList()
                }
                apprenticeAdapter =
                    MyApprenticeAdapter(this@MyApprenticeActivity, entity.disciples!!)
            }

            mVDB.rvApprentice.setAdapter(apprenticeAdapter)
        }

    }


}